﻿var Class = {
	create : function() {
		return function() {
			this.initialize.apply(this, arguments);
		};
	}
};
Object.extend = function(destination, source) {
	for ( var property in source) {
		destination[property] = source[property];
	}
	return destination;
};

/**
 * 使用ajax提交数据
 */
function ajax_post(the_url, the_param, succ_callback) {
	$.ajax({
		type : 'POST',
		url : the_url,
		data : the_param,
		success : succ_callback,
		error : function(html) {
			alert("提交数据失败，代码:" + html.status + "，请稍候再试");
		}
	});
}

var DEBUG=true;

var HOST = "/huas-traffic-information-service/";
// 判断是否正在删除关键点，防止controller面板单击事件业务处理
var deleting = false;

// 当前选择的操作
var currentOperation = 0;

// 道路集合
var roads = {
	cur : -1
};

var cur_area = {
	id : 1,
	name : '测试区',
	x : 0,
	y : 0
};

// 缩略图相对位置
var LEFT = 0;
var TOP = 0;

// 定义Location(坐标)对象
var Location = function(x, y, id, area) {
	if (typeof(y)!="undefined") {
		this.x = x;
		this.y = y;
	} else {
		if (typeof(x)!="undefined")
			if (typeof(x.clientX)!="undefined") {// Chrome
				this.x = x.clientX;
				this.y = x.clientY;
			} else {// FireFox
				this.x = Math.floor(x.clientX - controller.offset().left
						+ document.body.scrollLeft);
				this.y = Math.floor(x.clientY - controller.offset().top
						+ document.body.scrollTop);
			}
	}
	this.id = id ? id : 0;
	this.area = area ? area : cur_area;
};

var Lights;

var Light = Class.create();

Light.prototype = {
	initialize : function(option) {
		THIS_LIGHT = this;
		this.id = 0;
		this.locs = new ArrayList();
	},
	add : function(loc) {
		if (!this.locs || this.locs.size() == 8) {
			this.locs = new ArrayList();
		}
		this.locs.add(loc);
	},
	set : function(index,loc) {
		if (!this.locs || this.locs.size() == 8) {
			this.locs = new ArrayList();
		}
		this.locs.insertElementAt(index,loc);
	},
	toJSON : function() {
		var json = {};
		json.id = this.id;
		for(var i=this.locs.size();i<8;i++){
			this.locs.add(new Location(-1,-1,0,cur_area));
		}
		for(var i=0;i<this.locs.size();i++){
			if(typeof(this.locs.get(i))=="undefined" || this.locs.get(i)==null)
				this.locs.setElementAt(i,new Location(-1,-1,0,cur_area));
		}
		json.n1 = this.locs.get(0);
		json.n2 = this.locs.get(1);
		json.e1 = this.locs.get(2);
		json.e2 = this.locs.get(3);
		json.s1 = this.locs.get(4);
		json.s2 = this.locs.get(5);
		json.w1 = this.locs.get(6);
		json.w2 = this.locs.get(7);
		return json;
	},
	color : function(selector) {
		selector.removeClass("green");
		selector.each(function() {
			THIS_LIGHT.locs.foreach(function(i, d, l) {
				if (i == parseInt(l.attr("index"))) {
					if (l.attr("class").indexOf("grey") == -1 && d.x != -1
							&& d.y != -1) {
						l.addClass("green");
					}
				}
			}, $(this));
		});
	}
};

// 获取适当的id(按顺序连续产生0~n的id)
var getProperId = function(des, id) {
	if ($("#" + des + id).width()) {
		return getProperId(des, id + 1);
	} else {
		return id;
	}
};

/** ********定义道路对象******* */
var Road = Class.create();

Road.prototype = {
	// 对象初始化
	initialize : function(options) {
		this.color = options.color;
		this.id = options.id;
		this.name = options.name;
		if(typeof(options.locs)=="undefined")
			this.locs = new ArrayList();
		else
			this.locs = options.locs;
	},
	// 将关键点追加到道路最后
	addKeyPointToRoad : function(loc, callback) {
		if (this.locs.size() >= 0) {
			for ( var i = 0; i < this.locs.size(); i++) {
				if (this.locs.get(i).x == loc.x && this.locs.get(i).y == loc.y) {
					this.locs.remove(i);
					callback(this.locs);
					return;
				}
			}
			this.locs.add(loc);
			callback(this.locs);
		}
	},
	// 移除指定的关键点
	removeKeyPointFromRoad : function(index) {
		var temp = this.locs.get(index);
		this.locs.remove(index);
		return temp;
	},
	// 重新整理路径中的关键点
	getRoadLocs : function() {
		for (index in this.locs.data()) {
			if (!this.locs.get(index)) {
				alert(index + ' not exists');
			}
		}
		return this.locs.data();
	},
	showRoadsIn : function(selector) {
		var sr = selector;
		var list = this.locs;
		sr.html("");
		list.foreach(function(index, data) {
			index != list.size() - 1 ? sr.append("(" + data.x + "," + data.y
					+ ")>") : sr.append("(" + data.x + "," + data.y + ")");
		});
	},
	toJSON : function() {
		var json = {};
		if (this.id >= 0 && this.name && this.locs) {
			json = {
				id : this.id,
				name : this.name
			};
			var ls = new Array(this.locs.size());
			for ( var i = 0; i < this.locs.size(); i++) {
				var l = this.locs.get(i);
				ls[i] = {
					id : l.id,
					x : l.x,
					y : l.y,
					area : cur_area
				};
			}
			json.point = ls;
		}
		return json;
	}

};

// 获取URL变量值
function getUrlParam(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
	var r = window.location.search.substr(1).match(reg);
	if (r != null)
		return unescape(r[2]);
	return null;
}

/** **************动态加载js、css****************** */
$.extend({
	includePath : '',
	include : function(file) {
		var files = typeof file == "string" ? [ file ] : file;
		for ( var i = 0; i < files.length; i++) {
			var name = files[i].replace(/^\s|\s$/g, "");
			var att = name.split('.');
			var ext = att[att.length - 1].toLowerCase();
			var isCSS = ext == "css";
			var tag = isCSS ? "link" : "script";
			var attr = isCSS ? " type='text/css' rel='stylesheet' "
					: " language='javascript' type='text/javascript' ";
			var link = (isCSS ? "href" : "src") + "='" + $.includePath + name
					+ "'";
			if ($(tag + "[" + link + "]").length == 0)
				document.write("<" + tag + attr + link + "></" + tag + ">");
		}
	}
});

function excursion(canvas_id, img, x, y) {
	var c = document.getElementById(canvas_id).getContext("2d");
	c.drawImage(img, -x, -y);
}
